package com.yxw.live_vod_boot.service.impl;


import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ObjectUtil;
import java.util.List;
import java.util.UUID;
import java.util.ArrayList;
import java.math.BigDecimal;
import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.annotation.Async;


import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import com.yxw.yxnet_cd_center.common.utils.PasswordUtil;
import com.yxw.yxnet_cd_center.common.utils.BaseDateTime;

import com.yxw.live_vod_backend_facade.enums.LivePunishTypeEnum;
import com.yxw.live_vod_facade.dto.YxLiveShopPunishAddDTO;
import com.yxw.live_vod_boot.entity.YxLiveApplyEntity;
import com.yxw.live_vod_boot.entity.YxLiveAddressEntity;
import com.yxw.live_vod_boot.entity.YxLiveShopApplyEntity;
import com.yxw.live_vod_boot.entity.YxLiveShopUserEntity;
import com.yxw.live_vod_boot.entity.YxLiveShopEntity;
import com.yxw.live_vod_boot.entity.YxLiveViolationEntity;
import com.yxw.live_vod_boot.entity.YxLiveShopPunishEntity;
import com.yxw.live_vod_boot.mapper.YxLiveShopApplyMapper;
import com.yxw.live_vod_boot.mapper.YxLiveShopUserMapper;
import com.yxw.live_vod_boot.mapper.ShopMapper;
import com.yxw.live_vod_boot.mapper.YxLiveAddressMapper;
import com.yxw.live_vod_boot.mapper.YxLiveShopPunishMapper;
import com.yxw.live_vod_boot.mapper.YxLiveViolationMapper;
import com.yxw.live_vod_boot.service.LiveManagerProcessService;
import com.yxw.live_vod_boot.utils.RedisManager;
import com.yxw.live_vod_boot.utils.PageUtils;
import com.yxw.live_vod_boot.utils.NoticeTypeUtil;
import com.yxw.live_vod_boot.cache.QueueCache;
import com.yxw.live_vod_boot.entity.YxLiveDomainEntity;
import com.yxw.live_vod_boot.feign.BillingSetupServiceClient;
import com.yxw.live_vod_boot.mapper.YxLiveApplyMapper;
import com.yxw.live_vod_boot.utils.LiveStreamUtil;
import com.yxw.live_vod_boot.vo.CheckLiveIsActiveVO;
import com.yxw.live_vod_boot.vo.LiveInDelayCacheVO;
import com.yxw.live_vod_facade.dto.ManagerArrowLiveShopApplyDTO;
import com.yxw.live_vod_facade.dto.ManagerRefererLiveShopApplyDTO;
import com.yxw.live_vod_facade.constant.LiveShopConstant;
import com.yxw.live_vod_facade.dto.ManagerSendLiveQueryDTO;
import com.yxw.live_vod_facade.vo.LivePushStreamRedisCacheVO;
import com.yxw.live_vod_facade.enums.LiveStateEnum;
import com.yxw.live_vod_backend_facade.vo.NoticeTypeVo;
import com.yxw.live_vod_boot.dto.SendInteractMsgDto;
import com.yxw.live_vod_boot.mapper.LiveShopApplyAddressMapper;
import com.yxw.live_vod_facade.enums.NoticeTypeEnum;
import com.yxw.live_vod_boot.utils.JsonUtils;
import com.yxw.live_vod_boot.entity.LiveShopApplyAddressEntity;
/**
 * 直播管理逻辑服务，为后台feign调用
 * @author hjx
 * @date 2023-11-23
 */
@Slf4j
@Service
public class LiveSysManagerProcessServiceImpl  implements LiveManagerProcessService{
    
    @Autowired
    private RedisManager oRedisManager;
    
    @Autowired
    private YxLiveShopApplyMapper oYxLiveShopApplyMapper;
    
    @Autowired
    private YxLiveShopUserMapper oYxLiveShopUserMapper;
    
    @Autowired
    private ShopMapper oShopMapper;
    
    @Autowired
    private YxLiveAddressMapper oYxLiveAddressMapper;
    
    @Autowired
    private YxLiveShopPunishMapper oYxLiveShopPunishMapper;
    
    @Autowired
    private YxLiveApplyMapper oYxLiveApplyMapper;
    
    @Autowired
    private YxLiveViolationMapper oYxLiveViolationMapper;
    
    @Autowired
    private LiveProcessServiceImpl oLiveProcessServiceImpl;
    
    @Autowired
    private BillingSetupServiceClient billingSetupServiceClient;
    
    @Autowired
    private SysNoticeServiceImpl oSysNoticeServiceImpl;

    @Autowired
    private QueueCache oQueueCache;
    
    @Autowired
    private LiveShopApplyAddressMapper oLiveShopApplyAddressMapper;
    /*** 
    * 根据直播id查找直播详情
    * @param id
    * @return LiveShopApplyAddressEntity
    **/
    @Override
    public LiveShopApplyAddressEntity selectLiveShopApplyAddressById(String liveId)
    {
        if(ObjectUtil.isEmpty(liveId))
        {
            return null;
        }
        return oLiveShopApplyAddressMapper.selectById(liveId);
    }    
    /**
     * 管理员同意店铺开通直播权限
     * @param ManagerArrowLiveShopApplyDTO
     * @return BaseResult
     */ 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult managerArrowLiveShopApply(ManagerArrowLiveShopApplyDTO oManagerArrowLiveShopApplyDTO)
    {
        String sign = PasswordUtil.getFeignSign(oManagerArrowLiveShopApplyDTO.getAuditUser(),oManagerArrowLiveShopApplyDTO.getUpdateTime());
        if(!sign.equals(oManagerArrowLiveShopApplyDTO.getSignature()))
        {
            return BaseResult.failed("200", "非法操作");
        }
        YxLiveShopApplyEntity oYxLiveShopApplyEntity = oYxLiveShopApplyMapper.selectById(oManagerArrowLiveShopApplyDTO.getShopApplyId());
        if(null == oYxLiveShopApplyEntity)
        {
            return BaseResult.failed("200", "Id对应的申请不存在");
        }
        if(oYxLiveShopApplyEntity.getAuditResult() == 1)
        {
            return BaseResult.failed("200", "不能修改已经通过审核的申请");
        }        
        oYxLiveShopApplyEntity.setAuditResult(2);
        oYxLiveShopApplyEntity.setAuditUser(oManagerArrowLiveShopApplyDTO.getAuditUser());
        YxLiveShopUserEntity oYxLiveShopUserEntity = new YxLiveShopUserEntity();
        oYxLiveShopUserEntity.setCreateUserId(oYxLiveShopApplyEntity.getCreateUserId());
        oYxLiveShopUserEntity.setShopId(oYxLiveShopApplyEntity.getShopId());
        oYxLiveShopUserEntity.setShopUserId(oYxLiveShopApplyEntity.getCreateUserId());
        oYxLiveShopUserEntity.setUserType(oYxLiveShopApplyEntity.getShopAllowType());
        oYxLiveShopUserEntity.setCreateTime(BaseDateTime.getDate());
        YxLiveShopEntity oYxLiveShopEntity = new YxLiveShopEntity();
        oYxLiveShopEntity.setCreateTime(oYxLiveShopUserEntity.getCreateTime());
        oYxLiveShopEntity.setShopId(oYxLiveShopApplyEntity.getShopId());
        oYxLiveShopEntity.setShopBackground(oYxLiveShopApplyEntity.getLiveHomePicture());
        oYxLiveShopEntity.setSign(oYxLiveShopApplyEntity.getRemark());
        oYxLiveShopApplyMapper.updateById(oYxLiveShopApplyEntity);
        oYxLiveShopUserMapper.insert(oYxLiveShopUserEntity);
        oShopMapper.insert(oYxLiveShopEntity);
        oRedisManager.set(LiveShopConstant.LIVE_SHOP_APPLY_REDISKEY_PREFIX.concat(oYxLiveShopEntity.getShopId()), oYxLiveShopApplyEntity);
        String key = LiveShopConstant.LIVE_SHOP_USER_REDISKEY_PREFIX.concat(oYxLiveShopUserEntity.getLiveShopId().toString());
        oRedisManager.setMapValue(key,oYxLiveShopUserEntity.getShopUserId(), oYxLiveShopUserEntity);
        return BaseResult.success(oYxLiveShopApplyEntity);
    }            
    /**
     * 管理员拒绝店铺开通直播权限
     * @param ManagerRefererLiveShopApplyDTO
     * @return BaseResult
     */
    @Override
    public BaseResult managerRefererLiveShopApply(ManagerRefererLiveShopApplyDTO oManagerRefererLiveShopApplyDTO)
    {
        String sign = PasswordUtil.getFeignSign(oManagerRefererLiveShopApplyDTO.getAuditUser(),oManagerRefererLiveShopApplyDTO.getUpdateTime());
        if(!sign.equals(oManagerRefererLiveShopApplyDTO.getSignature()))
        {
            return BaseResult.failed("200", "非法操作");
        }
        YxLiveShopApplyEntity oYxLiveShopApplyEntity = oYxLiveShopApplyMapper.selectById(oManagerRefererLiveShopApplyDTO.getShopApplyId());
        if(null == oYxLiveShopApplyEntity)
        {
            return BaseResult.failed("200", "Id对应的申请不存在");
        }  
        if(oYxLiveShopApplyEntity.getAuditResult() == 2)
        {
            return BaseResult.failed("200", "不能修改已经通过审核的申请");
        }
        oYxLiveShopApplyEntity.setAuditResult(1);
        oYxLiveShopApplyEntity.setRefererCause(oManagerRefererLiveShopApplyDTO.getRefererCause());
        oYxLiveShopApplyEntity.setUpdateTime(BaseDateTime.getDate());
        oYxLiveShopApplyMapper.updateById(oYxLiveShopApplyEntity);
        return BaseResult.success(oYxLiveShopApplyEntity);
    }
    /**
     * 管理员查询直播
     * @param oManagerSendLiveQueryDTO
     * @return BaseResult
     */ 
    @Override
    public BaseResult managerSendLiveQuery(ManagerSendLiveQueryDTO oManagerSendLiveQueryDTO)
    {       
        QueryWrapper<LiveShopApplyAddressEntity> oYxLiveAddressQueryWrapper = new QueryWrapper();
        Page<LiveShopApplyAddressEntity> pageParams = new Page<>(oManagerSendLiveQueryDTO.getCurrPage(),oManagerSendLiveQueryDTO.getPageSize());
        oYxLiveAddressQueryWrapper.eq(ObjectUtil.isNotEmpty(oManagerSendLiveQueryDTO.getLiveId()),"live_id",oManagerSendLiveQueryDTO.getLiveId());
        oYxLiveAddressQueryWrapper.eq(ObjectUtil.isNotEmpty(oManagerSendLiveQueryDTO.getLiveShopId()),"live_shop_id",oManagerSendLiveQueryDTO.getLiveShopId());
        oYxLiveAddressQueryWrapper.like(ObjectUtil.isNotEmpty(oManagerSendLiveQueryDTO.getShopName()),"shop_name", oManagerSendLiveQueryDTO.getShopName());
        oYxLiveAddressQueryWrapper.like(ObjectUtil.isNotEmpty(oManagerSendLiveQueryDTO.getMerchantName()),"merchant_name",oManagerSendLiveQueryDTO.getMerchantName());        
        oYxLiveAddressQueryWrapper.eq(ObjectUtil.isNotEmpty(oManagerSendLiveQueryDTO.getLiveState()),"live_state",oManagerSendLiveQueryDTO.getLiveState());
        if(StrUtil.isNotBlank(oManagerSendLiveQueryDTO.getStartCreateTime()))
        {
            oYxLiveAddressQueryWrapper.ge(ObjectUtil.isNotEmpty(oManagerSendLiveQueryDTO.getStartCreateTime()),"push_start_server_time",BaseDateTime.getLongDate(oManagerSendLiveQueryDTO.getStartCreateTime()));
        }
        if(StrUtil.isNotBlank(oManagerSendLiveQueryDTO.getEndCreateTime()))
        {
            oYxLiveAddressQueryWrapper.le(ObjectUtil.isNotEmpty(oManagerSendLiveQueryDTO.getEndCreateTime()),"push_start_server_time",BaseDateTime.getLongDate(oManagerSendLiveQueryDTO.getEndCreateTime()));
        }
        return BaseResult.success(PageUtils.get(oLiveShopApplyAddressMapper.selectPage(pageParams, oYxLiveAddressQueryWrapper)));    
    }
    /*** 
    * 管理员处罚店铺
    * @param oYxLiveShopPunishDTO
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult managerPunishLiveShop(YxLiveShopPunishAddDTO oYxLiveShopPunishAddDTO)
    {
        Integer iRatio = 0;
        SendInteractMsgDto oSendInteractMsgDto = new SendInteractMsgDto();
        String content;
        YxLiveViolationEntity oYxLiveViolationEntity = oYxLiveViolationMapper.selectById(oYxLiveShopPunishAddDTO.getViolationId());
        if(null == oYxLiveViolationEntity)
        {
            return BaseResult.failed("根据Id找不到违规条例");
        } 
        if(oYxLiveViolationEntity.getPunishType().intValue() == LivePunishTypeEnum.LIVE_CURRENCY.getCode().intValue())
        {
            BaseResult ratioBaseResult = billingSetupServiceClient.getFeeDeductRatio();
            if(ratioBaseResult.isResult())
            {
                iRatio = (Integer)ratioBaseResult.getData();
            }
            else
            {
                return BaseResult.failed("200", "获取兑换率失败");
            }
            if(iRatio<1)
            {
                return BaseResult.failed("200", "兑换率不能小于1");
            }          
        }        
        YxLiveShopEntity oYxLiveShopEntity = oShopMapper.selectById(oYxLiveShopPunishAddDTO.getLiveShopId());
        if(null == oYxLiveShopEntity)
        {
            return BaseResult.failed("根据Id找不到店铺信息");
        } 
        YxLiveAddressEntity oYxLiveAddressEntity = oYxLiveAddressMapper.selectById(oYxLiveShopPunishAddDTO.getLiveId());
        if(null == oYxLiveAddressEntity)
        {
            return BaseResult.failed("根据Id找不到直播信息");
        }
        YxLiveApplyEntity oYxLiveApplyEntity = oYxLiveApplyMapper.selectById(oYxLiveAddressEntity.getApplyId());
        if(null == oYxLiveApplyEntity)
        {
            return BaseResult.failed("根据Id找不到直播申请信息");
        }
        oSendInteractMsgDto.setShopId(oYxLiveShopEntity.getLiveShopId());
        oSendInteractMsgDto.setShopName(oYxLiveShopEntity.getShopName());
        //oSendInteractMsgDto.setUserId(Long.valueOf(oYxLiveAddressEntity.getShopUserId()));
        oSendInteractMsgDto.setUserName(oYxLiveAddressEntity.getShopUserName());
        YxLiveDomainEntity oYxLiveDomainEntity = oLiveProcessServiceImpl.getPushDomain();
        String redisKey = LiveShopConstant.LIVE_PUSH_STREAM_CACHE_REDISKEY_PREFIX.concat(oYxLiveAddressEntity.getId());
        String redisLiveStateCheckKey = LiveShopConstant.LIVE_SHOP_ONLINELIVE_STATE_CHECK_PREFIX.concat(oYxLiveAddressEntity.getId());
        Integer liveState = (Integer)oRedisManager.get(redisLiveStateCheckKey);
        if(null == liveState)
        {
            liveState = oYxLiveAddressEntity.getLiveState();//LiveStateEnum.LIVE_END.getCode();
        }
        String mapKey = redisKey + "_" + UUID.randomUUID().toString().replaceAll("-", "");
        //LivePushStreamRedisCacheVO oLivePushStreamRedisCacheVO = (LivePushStreamRedisCacheVO)oRedisManager.get(redisKey);
        boolean isForbidStream = false;
        boolean isRedisSet = false;
        YxLiveShopPunishEntity oYxLiveShopPunishEntity = new YxLiveShopPunishEntity();//oYxLiveShopPunishAddDTO.ConverToEntity();
        oYxLiveShopPunishEntity.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        oYxLiveShopPunishEntity.setLiveShopId(oYxLiveShopPunishAddDTO.getLiveShopId());
        oYxLiveShopPunishEntity.setShopId(oYxLiveShopEntity.getShopId()) ;
        oYxLiveShopPunishEntity.setShopName(oYxLiveShopEntity.getShopName()) ;
        oYxLiveShopPunishEntity.setLiveId(oYxLiveShopPunishAddDTO.getLiveId()) ;
        oYxLiveShopPunishEntity.setMerchantId(oYxLiveShopEntity.getMerchantId()) ;
        oYxLiveShopPunishEntity.setMerchantName(oYxLiveShopEntity.getMerchantName()) ;
        oYxLiveShopPunishEntity.setViolationId(oYxLiveShopPunishAddDTO.getViolationId()) ;
        oYxLiveShopPunishEntity.setPunishDescription(oYxLiveShopPunishAddDTO.getPunishDescription());
        oYxLiveShopPunishEntity.setCreateTime(BaseDateTime.getDate()) ;        
        oYxLiveShopPunishEntity.setViolationReson(oYxLiveViolationEntity.getViolationReson());
        oYxLiveShopPunishEntity.setPunishType(oYxLiveViolationEntity.getPunishType());
        //oYxLiveShopPunishEntity.setPunishQuantity(oYxLiveViolationEntity.getPunishQuantity());
        oYxLiveShopPunishEntity.setPunishUnit(oYxLiveViolationEntity.getPunishUnit());
        oYxLiveShopPunishEntity.setLiveHomeName(oYxLiveApplyEntity.getLiveHomeName());
        oYxLiveShopPunishEntity.setLiveHomePicture(oYxLiveApplyEntity.getLiveHomePicture());
        oYxLiveShopPunishEntity.setLiveTime(BaseDateTime.getLongDateTimeString(oYxLiveApplyEntity.getLiveStartTime()));
        oSendInteractMsgDto.setBussId(oYxLiveShopPunishEntity.getId());
        oYxLiveAddressEntity.setPunishId(oYxLiveShopPunishEntity.getId());
        if(oYxLiveViolationEntity.getPunishType().intValue() == LivePunishTypeEnum.LIVE_CURRENCY.getCode().intValue())
        {  
            oSendInteractMsgDto.setType(NoticeTypeEnum.VIOLATION_PUNISH);
            //罚款
            int remainderFee;
            int deductionFee = oYxLiveViolationEntity.getPunishQuantity();
            //if(null != oLivePushStreamRedisCacheVO)
            //{
                oYxLiveAddressEntity.setLiveState(liveState);
            //}
            if(oYxLiveAddressEntity.getLiveState().intValue() == LiveStateEnum.LIVE_ONLINE.getCode().intValue())
            {
                remainderFee = oYxLiveShopEntity.getLiveCoin().intValue() - oYxLiveAddressEntity.getLiveValuation();
                if(remainderFee >= oYxLiveViolationEntity.getPunishQuantity())
                {
                    remainderFee = oYxLiveShopEntity.getLiveCoin().intValue() - oYxLiveViolationEntity.getPunishQuantity();
                    oYxLiveShopEntity.setLiveCoin(new BigDecimal(remainderFee));
                }
                else
                {
                    remainderFee = oYxLiveShopEntity.getLiveCoin().intValue();
                    if(remainderFee > oYxLiveViolationEntity.getPunishQuantity())
                    {
                        //提前罚款数量等值时间 结束直播，延时任务实现，如果有充值可以继续使用申请或再申请直播
                        remainderFee = oYxLiveShopEntity.getLiveCoin().intValue() - oYxLiveViolationEntity.getPunishQuantity();
                        oYxLiveShopEntity.setLiveCoin(new BigDecimal(remainderFee));
                        Long lTime = Long.valueOf(oYxLiveViolationEntity.getPunishQuantity() / iRatio);
                        lTime = lTime * 60000;
                        Long delayTime = oYxLiveAddressEntity.getLiveEndTime() - lTime;
                        CheckLiveIsActiveVO oCheckLiveIsActiveVO = new CheckLiveIsActiveVO();
                        oCheckLiveIsActiveVO.InIt(redisKey,mapKey,delayTime,2);
                        LiveInDelayCacheVO oLiveInCacheVO = new LiveInDelayCacheVO();
                        oLiveInCacheVO.setLiveId(oYxLiveAddressEntity.getId());
                        oLiveInCacheVO.setShopId(oYxLiveAddressEntity.getShopId());
                        oLiveInCacheVO.setLiveShopId(oYxLiveAddressEntity.getLiveShopId());
                        oLiveInCacheVO.setLiveApplyId(oYxLiveAddressEntity.getApplyId());
                        oLiveInCacheVO.setShopPunishId(oYxLiveShopPunishEntity.getId().concat(",").concat(oYxLiveDomainEntity.getDomainName()).concat(",").concat(oLiveProcessServiceImpl.getDefaultAppName()).concat(",").concat(oYxLiveAddressEntity.getLiveStreamName()));
                        oCheckLiveIsActiveVO.setLiveInCacheVO(oLiveInCacheVO);
                        oQueueCache.delayCheckLiveIsActivePut(oCheckLiveIsActiveVO);
                    }
                    else
                    {
                        //return BaseResult.failed("剩余视频币不够扣除处罚数量。");
                        //视频币不够罚款，直接结束直播，如果有充值可以继续使用申请或再申请直播
                        deductionFee = remainderFee;
                        oYxLiveShopEntity.setLiveCoin(BigDecimal.ZERO);
                        oYxLiveShopEntity.setUpdateTime(BaseDateTime.getDate());                    
                        isForbidStream = true;
                        liveState = LiveStateEnum.LIVE_STOP.getCode();
                        isRedisSet = true;
                        oYxLiveAddressEntity.setLiveState(LiveStateEnum.LIVE_STOP.getCode());
                        UpdateWrapper<YxLiveAddressEntity> oUpdateWrapper = new UpdateWrapper();
                        oUpdateWrapper.eq("live_id", oYxLiveAddressEntity.getId());
                        oUpdateWrapper.set("live_state", liveState);
                        oYxLiveAddressMapper.update(null, oUpdateWrapper);
                    }                    
                }
            }
            else
            {
                remainderFee = oYxLiveShopEntity.getLiveCoin().intValue();
                if(remainderFee > oYxLiveViolationEntity.getPunishQuantity())
                {
                    remainderFee = oYxLiveShopEntity.getLiveCoin().intValue() - oYxLiveViolationEntity.getPunishQuantity();
                    oYxLiveShopEntity.setLiveCoin(new BigDecimal(remainderFee));
                }
                else if(remainderFee > 0)
                {
                    //return BaseResult.failed("剩余视频币不够扣除处罚数量。");
                    deductionFee = remainderFee;
                    oYxLiveShopEntity.setLiveCoin(BigDecimal.ZERO);
                    oYxLiveShopEntity.setUpdateTime(BaseDateTime.getDate());                    
                    isForbidStream = true;
                    liveState = LiveStateEnum.LIVE_STOP.getCode();
                    isRedisSet = true;
                    UpdateWrapper<YxLiveAddressEntity> oUpdateWrapper = new UpdateWrapper();
                    oUpdateWrapper.eq("live_id", oYxLiveAddressEntity.getId());
                    oUpdateWrapper.set("live_state", liveState);
                    oYxLiveAddressMapper.update(null, oUpdateWrapper);
                }
            }
            oYxLiveShopPunishEntity.setPunishQuantity(deductionFee);
        }
        else if(oYxLiveViolationEntity.getPunishType().intValue() == LivePunishTypeEnum.LIVE_STREAM.getCode().intValue())
        {
            //中断
            oSendInteractMsgDto.setType(NoticeTypeEnum.LIVE_INTERRUPT);
            oYxLiveShopEntity.setShopState(1);
            isForbidStream = true;
            //if(null != oLivePushStreamRedisCacheVO)
            //{
                oYxLiveAddressEntity.setLiveState(liveState);
            //}
            if(oYxLiveAddressEntity.getLiveState().intValue() != LiveStateEnum.LIVE_END.getCode().intValue())
            {
                liveState = LiveStateEnum.LIVE_STOP.getCode();
                isRedisSet = true;

                //延时解除中断
                Long delayTime = Long.valueOf(oYxLiveViolationEntity.getPunishQuantity() * 60000);           
                CheckLiveIsActiveVO oCheckLiveIsActiveVO = new CheckLiveIsActiveVO();
                oCheckLiveIsActiveVO.InIt(redisKey,mapKey,delayTime,3);
                LiveInDelayCacheVO oLiveInCacheVO = new LiveInDelayCacheVO();
                oLiveInCacheVO.setLiveId(oYxLiveAddressEntity.getId());
                oLiveInCacheVO.setShopId(oYxLiveAddressEntity.getShopId());
                oLiveInCacheVO.setLiveShopId(oYxLiveAddressEntity.getLiveShopId());
                oLiveInCacheVO.setLiveApplyId(oYxLiveAddressEntity.getApplyId());
                oLiveInCacheVO.setShopPunishId(oYxLiveShopPunishEntity.getId().concat(",").concat(oYxLiveDomainEntity.getDomainName()).concat(",").concat(oLiveProcessServiceImpl.getDefaultAppName()).concat(",").concat(oYxLiveAddressEntity.getLiveStreamName()));
                oCheckLiveIsActiveVO.setLiveInCacheVO(oLiveInCacheVO);
                oQueueCache.delayCheckLiveIsActivePut(oCheckLiveIsActiveVO);   
                oYxLiveAddressEntity.setLiveState(LiveStateEnum.LIVE_STOP.getCode());
                oYxLiveAddressMapper.updateById(oYxLiveAddressEntity);
                UpdateWrapper<YxLiveApplyEntity> oLiveApplyUpdateWrapper = new UpdateWrapper();
                oLiveApplyUpdateWrapper.eq("apply_id", oYxLiveAddressEntity.getApplyId());
                oLiveApplyUpdateWrapper.set("live_state", LiveStateEnum.LIVE_STOP.getCode());
                oYxLiveApplyMapper.update(null, oLiveApplyUpdateWrapper);   
            }
        }
        else
        {
            //禁播
            oSendInteractMsgDto.setType(NoticeTypeEnum.LIVE_DISABLE);
            oYxLiveAddressEntity.setLiveState(LiveStateEnum.LIVE_LIMITED.getCode());
            oYxLiveShopEntity.setShopState(0);
            isForbidStream = true;
            //if(null != oLivePushStreamRedisCacheVO)
            //{
                oYxLiveAddressEntity.setLiveState(liveState);
            //}
            if(oYxLiveAddressEntity.getLiveState().intValue() != LiveStateEnum.LIVE_END.getCode().intValue())
            {

                liveState = LiveStateEnum.LIVE_LIMITED.getCode();
                isRedisSet = true;

                oYxLiveAddressMapper.updateById(oYxLiveAddressEntity);
                UpdateWrapper<YxLiveApplyEntity> oLiveApplyUpdateWrapper = new UpdateWrapper();
                oLiveApplyUpdateWrapper.eq("apply_id", oYxLiveAddressEntity.getApplyId());
                oLiveApplyUpdateWrapper.set("live_state", LiveStateEnum.LIVE_LIMITED.getCode());
                oYxLiveApplyMapper.update(null, oLiveApplyUpdateWrapper);
            }
                //延时解除禁播
                Long delayTime = Long.valueOf(oYxLiveViolationEntity.getPunishQuantity() * 86400000);           
                CheckLiveIsActiveVO oCheckLiveIsActiveVO = new CheckLiveIsActiveVO();
                oCheckLiveIsActiveVO.InIt(redisKey,mapKey,delayTime,4);
                LiveInDelayCacheVO oLiveInCacheVO = new LiveInDelayCacheVO();
                oLiveInCacheVO.setLiveId(oYxLiveAddressEntity.getId());
                oLiveInCacheVO.setShopId(oYxLiveAddressEntity.getShopId());
                oLiveInCacheVO.setLiveShopId(oYxLiveAddressEntity.getLiveShopId());
                oLiveInCacheVO.setLiveApplyId(oYxLiveAddressEntity.getApplyId());
                oLiveInCacheVO.setShopPunishId(oYxLiveShopPunishEntity.getId().concat(",").concat(oYxLiveDomainEntity.getDomainName()).concat(",").concat(oLiveProcessServiceImpl.getDefaultAppName()).concat(",").concat(oYxLiveAddressEntity.getLiveStreamName()));
                oCheckLiveIsActiveVO.setLiveInCacheVO(oLiveInCacheVO);
                oQueueCache.delayCheckLiveIsActivePut(oCheckLiveIsActiveVO);               
        }
        oYxLiveShopEntity.setUpdateTime(BaseDateTime.getDate());
        oShopMapper.updateById(oYxLiveShopEntity);
        oYxLiveShopPunishMapper.insert(oYxLiveShopPunishEntity);
        if(isRedisSet)
        {
            oRedisManager.set(redisKey, liveState);
        }
        if(isForbidStream)
        {  
            
            forbidStream(oYxLiveDomainEntity.getDomainName(), oLiveProcessServiceImpl.getDefaultAppName(), oYxLiveAddressEntity.getLiveStreamName());
        }
        //设置通知信息
        //content = oYxLiveShopPunishEntity.getPunishQuantity() + oYxLiveViolationEntity.getPunishUnit();
        NoticeTypeVo oNoticeTypeVo = NoticeTypeUtil.NoticeTypeEnumToVo(oSendInteractMsgDto, JsonUtils.toJson(oYxLiveShopPunishEntity));
        oSysNoticeServiceImpl.sendNoticeCustomNoticeType(oSendInteractMsgDto, oNoticeTypeVo);
        return BaseResult.success(oYxLiveShopPunishEntity);
    }
    /*
    不需要强制成功
    */
    @Transactional(rollbackFor = Exception.class)
    public BaseResult managerDeductFeeForbidStream(CheckLiveIsActiveVO oCheckLiveIsActiveVO,LivePushStreamRedisCacheVO oLivePushStreamRedisCacheVO)
    {
        LiveInDelayCacheVO oLiveInCacheVO = oCheckLiveIsActiveVO.getLiveInCacheVO();
        String redisKey = oCheckLiveIsActiveVO.getRedisKey();
        //LivePushStreamRedisCacheVO oLiveRedisCacheVO = (LivePushStreamRedisCacheVO)oRedisManager.get(redisKey);
        UpdateWrapper<YxLiveAddressEntity> oLiveUpdateWrapper = new UpdateWrapper();
        oLiveUpdateWrapper.eq("live_id", oLiveInCacheVO.getLiveId());
        oLiveUpdateWrapper.set("live_state", LiveStateEnum.LIVE_STOP.getCode());
        oYxLiveAddressMapper.update(null, oLiveUpdateWrapper);
        if(null != oLivePushStreamRedisCacheVO)
        {
            String redisLiveStateCheckKey = LiveShopConstant.LIVE_SHOP_ONLINELIVE_STATE_CHECK_PREFIX.concat(oLiveInCacheVO.getLiveId());
            Integer liveState = (Integer)oRedisManager.get(redisLiveStateCheckKey);
            if(null != liveState)
            {
                if(liveState.intValue() == LiveStateEnum.LIVE_ONLINE.getCode().intValue())
                {
                    liveState = LiveStateEnum.LIVE_STOP.getCode();
                    oRedisManager.set(redisLiveStateCheckKey, liveState);
                }                
            }            

        } 
        if(null != oCheckLiveIsActiveVO.getLiveInCacheVO())
        {
            if(null != oCheckLiveIsActiveVO.getLiveInCacheVO().getShopPunishId())
            {
                String[] aa = oCheckLiveIsActiveVO.getLiveInCacheVO().getShopPunishId().split(",");
                if(aa.length == 4)
                {
                    LiveStreamUtil.forbidStream(aa[1],aa[2],aa[3]);
                }
            }
        } 
        return BaseResult.success();
    }
    /*
    延时解除中断
    */
    @Transactional(rollbackFor = Exception.class)
    public BaseResult managerDelayResumeStop(CheckLiveIsActiveVO oCheckLiveIsActiveVO,LivePushStreamRedisCacheVO oLivePushStreamRedisCacheVO)
    {
        LiveInDelayCacheVO oLiveInCacheVO = oCheckLiveIsActiveVO.getLiveInCacheVO();
        String redisKey = oCheckLiveIsActiveVO.getRedisKey();
        //LivePushStreamRedisCacheVO oLiveRedisCacheVO = (LivePushStreamRedisCacheVO)oRedisManager.get(redisKey);
        YxLiveAddressEntity oYxLiveAddressEntity = oYxLiveAddressMapper.selectById(oLiveInCacheVO.getLiveId());
        YxLiveApplyEntity oYxLiveApplyEntity = oYxLiveApplyMapper.selectById(oLiveInCacheVO.getLiveApplyId());
        if(null != oYxLiveAddressEntity)
        {
            if(oYxLiveAddressEntity.getLiveState().intValue() != LiveStateEnum.LIVE_END.getCode().intValue())
            {
                UpdateWrapper<YxLiveAddressEntity> oLiveUpdateWrapper = new UpdateWrapper();
                oLiveUpdateWrapper.eq("live_id", oLiveInCacheVO.getLiveId());
                oLiveUpdateWrapper.set("live_state", LiveStateEnum.LIVE_FILED.getCode());
                oYxLiveAddressMapper.update(null, oLiveUpdateWrapper);
            }
        }
        if(null != oYxLiveApplyEntity)
        {
            if(oYxLiveApplyEntity.getLiveState().intValue() != LiveStateEnum.LIVE_END.getCode().intValue())
            {
                UpdateWrapper<YxLiveApplyEntity> oLiveApplyUpdateWrapper = new UpdateWrapper();
                oLiveApplyUpdateWrapper.eq("apply_id", oLiveInCacheVO.getLiveApplyId());
                oLiveApplyUpdateWrapper.set("live_state", LiveStateEnum.LIVE_FILED.getCode());
                oYxLiveApplyMapper.update(null, oLiveApplyUpdateWrapper);
            }
        }        
        UpdateWrapper<YxLiveShopEntity> oLiveShopUpdateWrapper = new UpdateWrapper();
        oLiveShopUpdateWrapper.eq("live_shop_id", oLiveInCacheVO.getLiveShopId());
        oLiveShopUpdateWrapper.set("shop_state", 2);
        oShopMapper.update(null, oLiveShopUpdateWrapper);
        if(null != oLivePushStreamRedisCacheVO)
        {
            String redisLiveStateCheckKey = LiveShopConstant.LIVE_SHOP_ONLINELIVE_STATE_CHECK_PREFIX.concat(oLiveInCacheVO.getLiveId());
            Integer liveState = (Integer)oRedisManager.get(redisLiveStateCheckKey);
            if(null != liveState)
            {
                liveState = LiveStateEnum.LIVE_FILED.getCode();
                oRedisManager.set(redisKey, liveState);
            }
        } 
        if(null != oCheckLiveIsActiveVO.getLiveInCacheVO())
        {
            if(null != oCheckLiveIsActiveVO.getLiveInCacheVO().getShopPunishId())
            {
                String[] aa = oCheckLiveIsActiveVO.getLiveInCacheVO().getShopPunishId().split(",");
                if(aa.length == 4)
                {
                    LiveStreamUtil.resumeStream(aa[1],aa[2],aa[3]);
                }
            }
        } 
        return BaseResult.success();
    } 
    /*
    延时解除禁播
    */    
    public BaseResult managerDelayResumeLimit(CheckLiveIsActiveVO oCheckLiveIsActiveVO)
    {
        LiveInDelayCacheVO oLiveInCacheVO = oCheckLiveIsActiveVO.getLiveInCacheVO();      
        UpdateWrapper<YxLiveShopEntity> oLiveShopUpdateWrapper = new UpdateWrapper();
        oLiveShopUpdateWrapper.eq("live_shop_id", oLiveInCacheVO.getLiveShopId());
        oLiveShopUpdateWrapper.set("shop_state", 2);
        int iCount = oShopMapper.update(null, oLiveShopUpdateWrapper);
        if(iCount == 1)
        {
            return BaseResult.success();
        }
        else
        {
            return BaseResult.failed("修改失败");
        }
    }     
    @Async
    private void forbidStream(String domainName,String appName,String streamName)
    {
         LiveStreamUtil.forbidStream(domainName, appName, streamName);
    }
    @Async
    private void resumeStream(String domainName,String appName,String streamName)
    {
         LiveStreamUtil.resumeStream(domainName, appName, streamName);
    }
}